﻿namespace Silverlight.Treemap.Extensions
{
    using System;
    using System.Windows;

    internal static class GetTemplateChildExtensions
    {
        public static T GetTemplateChild<T>(this IGetTemplateChild control, string childName) where T : DependencyObject
        {
            return GetTemplateChild<T>(control, childName, true);
        }

        public static T GetTemplateChild<T>(this IGetTemplateChild control, string childName, bool throwExceptionIfNotFound) where T : DependencyObject
        {
            var childControl = control.GetTemplateChildHelper(childName) as T;

            if (childControl == null && throwExceptionIfNotFound)
            {
                throw new Exception(string.Format("Unable to find {0} which is required.", childName));
            }

            return childControl;
        }
    }
}